/**
 * Created by baiguiren on 1/25/17.
 */

let async = require('async');
let fs = require('fs');

// passing a regular synchronous function
async.waterfall([
  async.apply(fs.readFile, __dirname + '/test.txt', 'utf8'),
  async.asyncify(JSON.parse),
  function (data, next) {
    // data is the result of parsing the text.
    // If there was a parsing error, it would have been caught
    next(null, data);
  }
], (err, result)=>{console.log(result.name)});

//console.log(JSON.parse('{"name": "awk", "age": "23"}'));

// passing a function returning a promise
async.waterfall([
  async.apply(fs.readFile, __dirname + '/test.txt', 'utf8'),
  async.asyncify((contents)=>{return contents;}),
  function (contents, next) {
    console.log(contents);
    next(null);
  }
], (err)=>{});

// es6 example
let q = async.queue(async.asyncify(async function (file) {
  let intermediateStep = await processFile(file);
  return await somePromise(intermediateStep);
}));